import {baseEffects, baseReducers, baseState, processFilter} from "../../../../models/baseModel";
import moment from "moment";
import $$ from "utils/index";
import * as request from "../../../../services/CommonHttpService";

export default {
  namespace: 'qualityDataChart',

  state: {
    ...baseState,
    data:{
      agv: 0,
      variance: 0.3,
      qualityChartDataTimeVos: []
    },
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getPageInfo({payload}, {call, put}) {

      const p = {
        ...payload,
        baseUrl: getBaseUrl(),
        process: (records) => {
          return records.map(record => {
            return {
              ...record,
              diff: Math.floor(Math.abs(record.value - record.agv) * 100) / 100,
              value: Math.floor(record.value * 100) / 100,
            };
          });
        }
      }

      yield put({
        type: 'baseGetPageInfo',
        payload: p
      });
    },

    * getData({payload}, {put, call}) {

      let {filters, errorMsg, notice, server} = payload;

      processFilter(filters);

      try {
        const data = [];
        let result = yield call(request.commonGet, "/quality-data/chart", {}, {...filters}, server);
        console.log("result", result);

        yield put({
          type: 'initState',
          payload: {data: result}
        });
      } catch (error) {
        console.error("getData", error);
        yield put({type: 'loadError'});
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
      }
    },

  }

}

function getBaseUrl() {
  return "/quality-data";
}
